package com.kay.springboot_es_api.utils;

import com.kay.springboot_es_api.domain.Filters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by Kay on 2018/11/6
 */
class SortBean{
    public String sort;
    public String order;
    public SortBean(String sort, String order){
        this.sort = sort;
        this.order = order;
    }

}
public class FilterUtil {
    private static Logger logger = LoggerFactory.getLogger(FilterUtil.class);
    // 根据传入的 参数格式化
    public static Map<String,Object> FilterParam(Filters filters)
    {
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("basic",0);
        params.put("diagnosis",0);
        params.put("medical",0);
        params.put("exam",0);
        // 获得性别
        if(filters.getSex()==null || filters.getSex().isEmpty()){
            logger.debug("sex empty");
        }else {
            params.put("sex" , Arrays.asList(filters.getSex().split(",")));
            params.put("basic",1);
        }

        //获得年龄范围
        if(filters.getAge()==null || filters.getAge().isEmpty()){
            logger.debug("age empty");
        }else {
            String[] ages = filters.getAge().split(";");   //   多组  范围
            List<Map<String,Integer>> ageRangeList = new ArrayList<Map<String, Integer>>();

            for (String age : ages)
            {
                List<String> ageList = Arrays.asList(age.split(",", -1));
                // 等于
                if(ageList.size() == 1){
                    Map<String,Integer> ageGroup = new HashMap<String, Integer>();
                    ageGroup.put("gte",Integer.parseInt(ageList.get(0)));
                    ageGroup.put("lte",Integer.parseInt(ageList.get(0)));
                    ageRangeList.add(ageGroup);
                }else if (ageList.size() == 2)
                {
                    if (ageList.get(0).isEmpty())  //   <=
                    {
                        Map<String,Integer> ageGroup = new HashMap<String, Integer>();
                        ageGroup.put("lte",Integer.parseInt(ageList.get(1)));
                        ageRangeList.add(ageGroup);
                    }else if (ageList.get(1).isEmpty()){   //  >=
                        Map<String,Integer> ageGroup = new HashMap<String, Integer>();
                        ageGroup.put("gte",Integer.parseInt(ageList.get(0)));
                        ageRangeList.add(ageGroup);
                    }else {  //  20,30   ==>  20<=  <=30
                        Map<String,Integer> ageGroup = new HashMap<String, Integer>();
                        ageGroup.put("gte",Integer.parseInt(ageList.get(0)));
                        ageGroup.put("lte",Integer.parseInt(ageList.get(1)));
                        ageRangeList.add(ageGroup);
                    }
                }
            }
            params.put("age" ,ageRangeList);
            params.put("basic",1);
        }

        // 获得 地区
        if(filters.getRegion()==null || filters.getRegion().isEmpty()){
            logger.debug("region empty");
        }else {
            params.put("region" ,Arrays.asList(filters.getRegion().split(",")));
            logger.debug("region: " + Arrays.asList(filters.getRegion().split(",")));
            params.put("basic",1);
        }

        //疾病 诊断
        if(filters.getDisease()==null || filters.getDisease().isEmpty()){
            logger.debug("disease empty");
        }else {
            try {
                String[] diseaseGroup = filters.getDisease().split(";", -1);   // 分隔出疾病的 code  和name
                if(diseaseGroup.length == 2) {
                    params.put("disease_code", diseaseGroup[0].split(","));
                    params.put("disease_name", diseaseGroup[1].split(","));
                    params.put("diagnosis",1);
//                    logger.info("diseaseGroup[0] : " + diseaseGroup[0].toString());
//                    logger.info("diseaseGroup[1] : " + diseaseGroup[1].toString());
                }else
                {
                    logger.debug("disease param error!");
                }
                String[] dignosisTime = filters.getDignosisTime().split(",",-1);  // 诊断时间 startDignosisTime  endDignosisTime
                if(dignosisTime.length == 2){

                    params.put("startDignosisTime",dignosisTime[0]);
                    params.put("endDignosisTime",dignosisTime[1]);
                    params.put("diagnosis",1);
                    logger.info("dignosisTime 0: " + dignosisTime[0] + "  1: " + dignosisTime[1]);
                }else
                {
                    logger.debug("DignosisTime param error!");
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }
        //药品 只查询 code
        if(filters.getMedical()==null || filters.getMedical().isEmpty()){
            logger.debug("medical empty");
        }else {
            params.put("medical_code" ,filters.getMedical().split(","));
            params.put("medical",1);
        }

        // sortColumn
        if(filters.getSortColumn()==null || filters.getSortColumn().isEmpty()){
            logger.debug("sortColumn empty");
        }else {
            String[] sortColumns = filters.getSortColumn().split("\\|");
            String[] orders = filters.getSortOrder().split("\\|");
            int sortLength = sortColumns.length;
            int orderLength = orders.length;
//            List<Map<String,String>> sortList = new ArrayList<Map<String,String>>();
            List<SortBean> sortList = new ArrayList<SortBean>();
            if( sortLength <= orderLength)  //  order 多
            {
                for (int i =0 ; i< sortLength ; i++){
//                    sortMap.put(sortColumns[i],orders[i]);
                    Map<String,String> sortMap = new HashMap<String,String>();
                    sortMap.put(sortColumns[i],orders[i]);

//                    sortList.add(sortMap);
                    sortList.add(new SortBean(sortColumns[i],orders[i]));
                }
            }else {  // sortLength  > orderLength 不足的 默认 desc
                for( int i = 0 ; i< orderLength ; i++){
                    Map<String,String> sortMap = new HashMap<String,String>();
                    sortMap.put(sortColumns[i],orders[i]);
//                    sortList.add(sortMap);
                    sortList.add(new SortBean(sortColumns[i],orders[i]));
                }
                for(int j = orderLength ; j< sortLength; j++){
                    Map<String,String> sortMap = new HashMap<String,String>();
                    sortMap.put(sortColumns[j],"desc");
//                    sortList.add(sortMap);
                    sortList.add(new SortBean(sortColumns[j],"desc"));
                }
            }
            logger.info("sortList : " + sortList.toString());
            params.put("sortColumn" ,sortList);
        }

        // sortOrder
//        if(filters.getSortOrder()==null || filters.getSortOrder().isEmpty()){
//            logger.debug("sortOrder empty");
//        }else {
//            params.put("sortOrder" ,filters.getSortOrder().split("\\|"));
//        }

        //默 认  from  = 0  size = 20
        Long size = 20l;
        Long from = 0l;
        //pageSize
        if(filters.getPageSize()==null ){
            logger.debug("pageSize empty");
        }else {
            size = filters.getPageSize();
            //currentPage
            if(filters.getCurrentPage()==null ){
                logger.debug("currentPage empty");
            }else {
                from = (filters.getCurrentPage() - 1) * size ;
                params.put("from" ,from);
                params.put("size" ,size);
            }
        }
        params.put("from" ,from);
        params.put("size" ,size);

        logger.debug("params : " + params.toString());
        logger.info("filters string : " + filters.toString());
        logger.info("params :" + params.toString());
        return params;
    }
}
